package com.zb.service.imp.base;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.zb.dao.ext.base.OmsStoreFileDao;
import com.zb.entity.base.OmsStoreFile;
import com.zb.entity.exception.OmsDaoException;

public  class FileBaseService extends BaseService
{
	protected OmsStoreFileDao<OmsStoreFile> storeFileDaoImpl;

	protected OmsStoreFile  genarateObj(String fileId,String entity,String fileFun,String creatId,String originalFilename,String downloadPath,String localPath,String entityId) 
	{
		OmsStoreFile storeFile = new OmsStoreFile(); 
		storeFile.setFile_id(fileId);
		storeFile.setDownload_path(downloadPath);
		storeFile.setRemote_file_name(localPath);
		storeFile.setCreate_oper_id(creatId);
		storeFile.setCreate_time(new Date());
		storeFile.setFilename_extension(getExtensionName(originalFilename));
		storeFile.setFile_name(originalFilename);
		storeFile.setEntity_tbl(entity);
		storeFile.setValid_flag("1");
		storeFile.setFile_function(fileFun);
		storeFile.setEntity_id(entityId);
		return storeFile;
	}
	/**
	 * 因为需要查询生成最大文件Id
	 * @param oms
	 * @throws Exception
	 */
	protected synchronized void saveFile(OmsStoreFile oms) throws Exception 
	{
		String maxId = null;
		maxId = storeFileDaoImpl.findMaxId(OmsStoreFile.class);
		maxId = this.genarateId(maxId, 0);
		if (maxId != null)
		{
			oms.setFile_id(maxId);
			storeFileDaoImpl.add(oms);
		}
	}
	
	protected void updateFile(OmsStoreFile oms) throws OmsDaoException
	{
		if (StringUtils.isNotEmpty(oms.getFile_id()))
		{
			storeFileDaoImpl.update(oms);
		}
	}
	
	protected  String getExtensionName(String filename) { 
        if ((filename != null) && (filename.length() > 0)) { 
            int dot = filename.lastIndexOf('.'); 
            if ((dot >-1) && (dot < (filename.length() - 1))) { 
                return filename.substring(dot + 1); 
            } 
        } 
        return filename; 
    } 

	public OmsStoreFileDao<OmsStoreFile> getStoreFileDaoImpl() {
		return storeFileDaoImpl;
	}

	public void setStoreFileDaoImpl(OmsStoreFileDao<OmsStoreFile> storeFileDaoImpl) {
		this.storeFileDaoImpl = storeFileDaoImpl;
	}
	
	
}
